#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
//加报头 eg:12\r\n{json}\r\n
const std::string Sep="\r\n";
bool Encode(std::string &message)
{
  if(message.size()==0) return false;
  std::string pacakge = std::to_string(message.size())+Sep+message+Sep;
  message=pacakge;
  return true;
}
//从pacakge中解析出json串放到connet中输出给外部
bool Decode(std::string & pacakge,std::string * content)
{
  auto pos = pacakge.find(Sep);
  if(pos==std::string::npos)
  {
    return false;
  }
  std::string content_length_str = pacakge.substr(0,pos);
  int content_length = std::stoi(content_length_str);
  //以\r\n分割的一组串的值 传进来的pacakge的长度至少要大于该值 要不报文不完整无法解析
  int full_length = content_length_str.size()+Sep.size()*2+content_length;
  if(pacakge.size()<full_length)
  {
    return false;
  }
  *content = pacakge.substr(pos+Sep.size(),content_length);
  pacakge.erase(0,full_length);
  return true;
}
class Request
{
public:
  Request():_x(0),_y(0),_oper(0)
  {
  }
  Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
  {
  }
  ~Request()
  {
  }
  bool Serialization(std::string &out_string)
  {
    Json::Value root;
    root["x"] = _x;
    root["y"] = _y;
    root["oper"] = _oper;
    Json::StreamWriterBuilder wbuilder; // StreamWriter的工厂
    std::unique_ptr<Json::StreamWriter> w(wbuilder.newStreamWriter());
    std::stringstream ss;
    w->write(root, &ss);
    // std::cout << ss.str() << std::endl;
    out_string = ss.str();
    return true;
  }
  bool DeSerialization(std::string &in_string)
  {
    Json::Reader reader;
    Json::Value root;
    // 从字符串中读取 JSON 数据
    bool parsingSuccessful = reader.parse(in_string, root);
    if (!parsingSuccessful)
    {
      // 解析失败，输出错误信息
      std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
      return false;
    }
    _x = root["x"].asInt();
    _y = root["y"].asInt();
   _oper = root["oper"].asInt();  
    return true;
  }
  int X() const
  {
    return _x;
  }
  int Y() const
  {
    return _y;
  }
  char Oper() const
  {
    return _oper;
  }
  
private:
  int _x;
  int _y;
  char _oper;
};

class Respond
{
public:
  Respond():_result(0),_code(0)
  {
  }
  ~Respond()
  {
  }
  bool Serialization(std::string &out_string)
  {
    Json::Value root;
    root["result"] = _result;
    root["code"] = _code;
    Json::StreamWriterBuilder wbuilder; // StreamWriter的工厂
    std::unique_ptr<Json::StreamWriter> w(wbuilder.newStreamWriter());
    std::stringstream ss;
    w->write(root, &ss);
    // std::cout << ss.str() << std::endl;
    out_string = ss.str();
    return true;
  }

  bool DeSerialization(std::string &in_string)
  {
    Json::Reader reader;
    Json::Value root;
    // 从字符串中读取 JSON 数据
    bool parsingSuccessful = reader.parse(in_string, root);
    if (!parsingSuccessful)
    {
      // 解析失败，输出错误信息
      std::cout << "Failed to parse JSON: " << reader.getFormattedErrorMessages() << std::endl;
      return false;
    }
    _result = root["result"].asInt();
    _code = root["code"].asInt();
    return true;
  }
  int Result() const
  {
    return _result;
  }
  int Code() const
  {
    return _code;
  }
  void SetResult(int res)
  {
    _result = res;
  }
  void SetCode(int code)
  {
    _code = code;
  }


private:
  int _result;//存放储存结果
  int _code;//存放出错码
};